package com.saibo.spider.gather;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import com.alibaba.fastjson.JSON;
import com.saibo.spider.commons.ConnectServer;
import com.saibo.spider.pojo.TaskEntry;
import com.saibo.spider.utils.HttpRequestUtil;
import com.saibo.spider.utils.HttpResponseUtil;
import com.saibo.spider.utils.JsoupUtil;
import com.saibo.spider.utils.StringUtil;

public class MouldBBSAutohomeCar implements CrawlerInterface {
	Map<String,Object> mso ;
	String url;
	String html;
	
	public void startMission(String url, String mapInfo,Submitable submitobj) {
		// TODO Auto-generated method stub
		this.url = url;
		mso = StringUtil.jsonToMap(mapInfo);
		int page = Integer.parseInt(mso.get("pageNumber").toString());
		for(int i = 0;i<page;i++){
			//请求
			requestUrl();
			//解析
			parse();
			try {
				Thread.sleep(10*1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	public void requestUrl() {
		// TODO Auto-generated method stub
		html = connect(url);
	}

	public void parse() {
		Document document = Jsoup.parse(html);
		List<Element> articleList = document.select("#subcontent>dl");
		List<TaskEntry> list = new ArrayList<TaskEntry>();
		for (Element element : articleList) {
			TaskEntry task = new TaskEntry();
			Element titleEle = element.select("dl>dt>a.a_topic").first();
			String title = JsoupUtil.element2text(titleEle);
			if(StringUtil.isNullOrEmpty(titleEle)){
				continue;
			}
			String url = titleEle.attr("href");
			//System.out.println("标题:"+title+"\t文章地址:"+url);
			
			int actionID = Integer.parseInt(mso.get("classinfoID").toString());
			int taskID = Integer.parseInt(mso.get("taskID").toString());
			task.setEntryUrl("http://club.autohome.com.cn"+url);
			task.setActionID(actionID);
			task.setTaskID(taskID);
			task.setScore(1.0);
			task.setInitAction("汽车之家论坛帖子采集");
			list.add(task);
		}
		System.out.println("提交了:"+list.size()+"个任务");
		String submiturl = "http://192.168.1.167:8081/services/schedule/addEntry4http";
		ConnectServer.sendPostRequest(submiturl, StringUtil.objectToJson(list));
		//下一页
		Element ele_next = document.select("#subcontent>div.pagearea>div.pages>a.afpage").first();
		url = "http://club.autohome.com.cn"+ele_next.attr("href");
	}

	public void submit() {
		
	}
	
	
	
	
	
	
	public static void main(String[] args) {
		MouldBBSAutohomeCar a = new MouldBBSAutohomeCar();
		String html = a.connect("http://club.autohome.com.cn/bbs/forum-c-770-1.html");
		a.parseIndex(html);
	}
	
	public String connect(String url){
		CloseableHttpResponse httpResponse = HttpRequestUtil.getRequestMethod(url, requestHeader());
		String html = HttpResponseUtil.getHtml(httpResponse,"gb2312");
		return html;
	}
	
	public void parseIndex(String html){
		Document document = Jsoup.parse(html);
		List<Element> articleList = document.select("#subcontent>dl");
		for (Element element : articleList) {
			Element titleEle = element.select("dl>dt>a.a_topic").first();
			String title = JsoupUtil.element2text(titleEle);
			if(StringUtil.isNullOrEmpty(titleEle)){
				continue;
			}
			String url = titleEle.attr("href");
			
			Element authorEle = element.select("dl>dd>a.linkblack").first();
			String author = JsoupUtil.element2text(authorEle);
			String authorUrl = authorEle.attr("href");
			
			Element tdateEle = element.select("dl>dd>span.tdate").first();
			String createdate = JsoupUtil.element2text(tdateEle);
			
			Element langEle = element.select("dl").first();
			String lang = langEle.attr("lang");
			
			Element reAuthor = element.select("dl>dd>a.linkblack").get(1);
			String reAuthorName = StringUtil.element2text(reAuthor);
			String reAuthorUrl = reAuthor.attr("href");
			Element reDate = element.select("dl>dd>span.ttime").first();
			String reDateTime = StringUtil.element2text(reDate);
			
			System.out.println("标题:"+title+"\t文章地址:"+url);
			System.out.println("作者:"+author+"\t作者主页:"+authorUrl+"\t发文日期:"+createdate);
			System.out.println("最后回复:"+reAuthorName+"\t回复人主页:"+reAuthorUrl+"\t回复日期:"+reDateTime);
			System.out.println("附加参数:"+lang);
			/////////////////////////////////////////////
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
			Date date = null;
			try {
				date = format.parse(createdate);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			Map<String,String> postinfo = new HashMap<String,String>();
			postinfo.put("parent_url", "");
			postinfo.put("domain", "autohome.com");
			postinfo.put("user_url", authorUrl);
			postinfo.put("user_nickname", author);
			postinfo.put("user_headimg", "");
			postinfo.put("createdate", date.getTime()+"");
			postinfo.put("url", "http://club.autohome.com.cn"+url);
			postinfo.put("content", title);
			
			List<Map<String,String>> ls = new ArrayList<Map<String,String>>();
			ls.add(postinfo);
			
			
			String url111 = "http://192.168.1.213:8080/aspirator-stream/stream/dataSubmit/addData";
			Map<String,Object> da = new HashMap<String,Object>();
			da.put("TABLE", "original_data");
			da.put("_SOURCE", "http://club.autohome.com.cn"+url);
			da.put("DATA", ls);
			da.put("intoBloom", "0");
			
			
			Map<String,String> mso = new HashMap<String,String>();
			
			List<Map<String,Object>> s = new ArrayList<Map<String,Object>>();
			s.add(da);
			mso.put("jsonData", JSON.toJSONString(s));
			System.err.println(JSON.toJSON(mso));
			System.out.println(HttpResponseUtil.getHtml(HttpRequestUtil.postRequestMethod(url111, null, mso)));
		}
		//下一页
		Element ele_next = document.select("#subcontent>div.pagearea>div.pages>a.afpage").first();
		System.out.println("下一页:"+ele_next.attr("href"));
	}
	
	public Map<String,String> requestHeader(){
		Map<String,String> mso = new HashMap<String,String>();
		mso.put("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
		mso.put("Accept-Encoding", "gzip, deflate, sdch");
		mso.put("Accept-Language", "zh-CN,zh;q=0.8");
		mso.put("Cache-Control", "max-age=0");
		mso.put("Connection", "keep-alive");
		mso.put("Cookie", "CYHTooltip=1; sessionfid=913430254; qasteam21239314=0; cookieCityId=110100; fvlid=1479709625240Zl0kn2IT; sessionid=84D31B8A-7C16-48AC-A35F-F9D9A0990579%7C%7C2016-11-21+14%3A27%3A06.736%7C%7C0; pcpopclub=FC051E5B3C417087A1C827BC4E965812C8304DD4B364F438678771252E9F317B38960EF36D00FD84FAB94C84192A5844B2F1BD283B478EF89FE8A921DDFC6C9523CB19615E03264CE07DA7FDF3E9788560C9C28A5449E6692A8FA1F368FBB930379ABA1E8CCCDA580F354B7A129DFCC526A3DE825C406B0F63135F023251A44FC5FB3F17E60D7C29CE1160369F56F14755A8D66296C778CAFB1377508DD987B53608383D55D9FFDC8142136F05F711B8E6D98370B91F86E1BFDB0B1CCE3B9AE410883C392D6531714AE4582F45FDD91FC814642C5BC5253BBB4EA592B79210540BB4FEEAF864C84F77D9048E2951A7AFD1E6CC9E68DED2C16A4BC7D8FE212B0BFBE80D3315FA802A45FD00261B64819A00E9FE687111E9642944FF3E135B75781079754A608772F4FA71CE8E39F422482815189D0195851AB897CE13D4A8F49A096D7593; clubUserShow=21239314|3207|2|%e6%88%91%e7%9c%9f%e6%98%af%e5%b0%91%e5%85%88%e9%98%9f%e5%91%98|0|0|1|userheaders/g8/M15/F8/E4/120X120_0_q87_autohomecar__wKgHz1eqzkeAbkqJAAFwe0H0duc557.jpg|2016-11-30 10:25:08|0; autouserid=21239314; sessionuid=84D31B8A-7C16-48AC-A35F-F9D9A0990579||2016-11-21+14%3A27%3A06.736||0; sessionip=124.205.63.255; sessionfid=913430254; sessionuserid=21239314; sessionlogin=9376d57490464937abae267a7d5647d901441612; historybbsName4=c-153%7C%E5%AE%9D%E9%A9%AC7%E7%B3%BB%2Cc-117%7C%E8%92%99%E8%BF%AA%E6%AC%A7%2Cc-588%7C%E5%A5%94%E9%A9%B0C%E7%BA%A7%2Cc-2838%7CMacan%2Cc-770%7CRAV4%E8%8D%A3%E6%94%BE%2Cc-375%7C%E9%94%90%E5%BF%97; ahpvno=27; ref=www.baidu.com%7C0%7C0%7Cwww.google.com%7C2017-03-09+15%3A40%3A52.227%7C2017-02-08+11%3A24%3A19.561; sessionvid=CDADC557-0401-484B-91E9-B75F55932626; area=110199; __utma=1.1174897996.1479709627.1489032164.1489041642.40; __utmb=1.0.10.1489041642; __utmc=1; __utmz=1.1489032164.39.25.utmcsr=i.autohome.com.cn|utmccn=(referral)|utmcmd=referral|utmcct=/receivereply/; ahrlid=14890452857251zamL9oP-1489045418029");
		mso.put("Host", "club.autohome.com.cn");
		mso.put("Upgrade-Insecure-Requests", "1");
		mso.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36");
		return mso;
	}

	
}
